// components/DeviceTest.tsx
import React, { useEffect } from 'react';
import { View, Text, Button } from 'react-native';
import { getDeviceTypes, getDevices, createDevice, getDeviceDetails, deleteDevice, updateDeviceState, getDeviceStateHistory } from '../../api/device';

const DeviceTest = () => {
  const testGetDevices = async () => {
    try {
      const response = await getDevices();
      console.log('Get devices response:', response.data);
    } catch (error) {
      console.error('Get devices error:', error);
    }
  };

  const testGetDeviceTypes = async () => { 
    try {
      const response = await getDeviceTypes();
      console.log('Get device types response:', response.data);
    } catch (error) {
      console.error('Get device types error:', error);
    }
  };

  const testCreateDevice = async () => {
    try {
      const response = await createDevice({
        name: 'ACC',
        type_id: 4,
        family_id: '54411c5c03e44bb68fec14d5bfb5571c',
        meta: { location: 'Living Room' },
        // state: { is_on: false }
      });
      console.log('Create device response:', response.data);
    } catch (error) {
      console.error('Create device error:', error);
    }
  };

  const testGetDeviceDetails = async () => {
    try {
      const response = await getDeviceDetails('125');
      console.log('Get device details response:', response.data);
    } catch (error) {
      console.error('Get device details error:', error);
    }
  };

  const testDeleteDevice = async () => {
    try {
      const response = await deleteDevice('your-device-id');
      console.log('Delete device response:', response.data);
    } catch (error) {
      console.error('Delete device error:', error);
    }
  };

  const testUpdateDeviceState = async () => {
    try {
      const response = await updateDeviceState('20', { is_on: true });
      console.log('Update device state response:', response.data);
    } catch (error) {
      console.error('Update device state error:', error);
    }
  };

  const testGetDeviceStateHistory = async () => {
    try {
      const response = await getDeviceStateHistory('20', {
        start_time: '2025-05-01T00:00:00Z',
        end_time: '2025-05-31T23:59:59Z'
      });
      console.log('Get device state history response:', response.data);
    } catch (error) {
      console.error('Get device state history error:', error);
    }
  };

  useEffect(() => {
    testGetDevices();
    testCreateDevice();
    testGetDeviceDetails();
    testDeleteDevice();
    testUpdateDeviceState();
    testGetDeviceStateHistory();
    testGetDeviceTypes();
  }, []);

  return (
    <View>
      <Text>Device API Test</Text>
      <Button title="Get Devices" onPress={testGetDevices} />
      <Button title="Create Device" onPress={testCreateDevice} />
      <Button title="Get Device Types" onPress={testGetDeviceTypes} />
      <Button title="Get Device Details" onPress={testGetDeviceDetails} />
      <Button title="Delete Device" onPress={testDeleteDevice} />
      <Button title="Update Device State" onPress={testUpdateDeviceState} />
      <Button title="Get Device State History" onPress={testGetDeviceStateHistory} />
    </View>
  );
};

export default DeviceTest;